package com.business.business.web.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.business.business.common.base.BaseService;
import com.business.business.model.Business;
import com.business.business.support.model.RespEntity;
import com.business.business.support.mybatisplus.QueryWrapper;
import com.business.business.support.utils.https.StringUtil;
import com.business.business.support.utils.requestutil.HttpRequestUtil;
import com.business.business.web.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xulumin
 * @Date 2024/3/21 08:49
 * @Version 1.0
 * @Desc
 */
@Service
public class EmgInfoService extends BaseService {
    @Resource
    private EmgProfessorMapper professorMapper;

    @Resource
    private EmgTeamMapper emgTeamMapper;

    @Resource
    private EmgPlaceMapper emgPlaceMapper;

    @Resource
    private EmgMaterialsMapper emgMaterialsMapper;

    @Resource
    private BusinessMapper businessMapper;

    /**
     * Author: xulumin
     * Date: 2024/3/21
     * Description: 获取应急保障情况信息
     */
    public RespEntity getEmgInfo(JSONObject jsonObject) {
        //处理支持form-data和json两种方式
        jsonObject = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonObject, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");

        // 根据地区获取企业列表
        String areaId = jsonObject.getStr("areaId");
        if (!StringUtil.strIsNullOrEmpty(areaId)) {
            LambdaQueryWrapper<Business> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.like(Business::getArea, areaId).select(Business::getId);
            List<Business> businesses = businessMapper.selectList(lambdaQueryWrapper);
            List<String> ids = new ArrayList<>();
            for (Business business : businesses) {
                ids.add(business.getId());
            }
            Map<String, Object> map = new HashMap<>();
            if (businesses.isEmpty()) {
                // 专家数量
                map.put("proNumber", 0);
                // 队伍数量
                map.put("teamNumber", 0);
                // 场所数量
                map.put("placeNumber", 0);
                // 物资总数量
                map.put("count", 0);
                // 物资灭火及爆炸物处置设备
                map.put("countFire", 0);
                // 物资安防及反恐防爆装备
                map.put("countTerror", 0);
                // 物资粮食
                map.put("countCereal", 0);
                // 物资除粮食之外的食品
                map.put("countFood", 0);
                // 物资其他
                map.put("countOther", 0);
                return RespEntity.success(map);
            }

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("business_id", ids);
            // 开始查询
            int proNumber = professorMapper.selectCount(queryWrapper);
            int teamNumber = emgTeamMapper.selectCount(queryWrapper);
            int placeNumber = emgPlaceMapper.selectCount(queryWrapper);
            // 物资总数量
            Object count = emgMaterialsMapper.selectSum(ids);
            Object countFire = emgMaterialsMapper.selectSumType(ids, 1);
            Object countTerror = emgMaterialsMapper.selectSumType(ids,2);
            Object countCereal = emgMaterialsMapper.selectSumType(ids, 3);
            Object countFood = emgMaterialsMapper.selectSumType(ids,4);
            Object countOther = emgMaterialsMapper.selectSumType(ids,5);


            // 专家数量
            map.put("proNumber", proNumber);
            // 队伍数量
            map.put("teamNumber", teamNumber);
            // 场所数量
            map.put("placeNumber", placeNumber);
            // 物资总数量
            map.put("count", count);
            // 物资灭火及爆炸物处置设备
            map.put("countFire", countFire);
            // 物资安防及反恐防爆装备
            map.put("countTerror", countTerror);
            // 物资粮食
            map.put("countCereal", countCereal);
            // 物资除粮食之外的食品
            map.put("countFood", countFood);
            // 物资其他
            map.put("countOther", countOther);
            return RespEntity.success(map);
        }

        return RespEntity.fail("请填写区域id");

    }

}
